package com.xnfh.home.viewmodel

import com.huantansheng.easyphotos.models.album.entity.Photo
import com.xnfh.common.manager.UserInfoManager
import com.xnfh.framework.base.BaseViewModel
import com.xnfh.framework.bean.FeedBackFieldBean
import com.xnfh.framework.bean.RespStateData
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2024/1/4 15:29
 * @UpdateUser: ziheng
 * @UpdateDate: 2024/1/4 15:29
 * @UpdateRemark: 更新说明
 */
class FeedBackViewModel : BaseViewModel() {
    private val viewModelRes = FeedBackViewModelRes()
    val fieldList = RespStateData<List<FeedBackFieldBean>>()
    val feedbackMessage = RespStateData<Any>()

    /**
     * 获取反馈列表
     */
    fun requestFeedBackFieldList() = launch {
        viewModelRes.requestFeedBackFieldList(fieldList)
    }

    /**
     * 提交反馈
     */
    fun requestFeedBack(
        photoList: ArrayList<Photo>,
        cropId: Int?,
        selectFieldId: String?,
        provinceId: String?,
        cityId: String?,
        districtId: String?,
        feedbackType: Int?,
        feedbackMes: String
    ) = launch {
        //1.创建MultipartBody.Builder对象
        val builder = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
        photoList.forEach {
            val file = File(it.path)
            val body: RequestBody =
                RequestBody.create(MediaType.parse("multipart/form-data"), file) //表单类型
            builder.addFormDataPart("file",file.name,body)
        }
        builder.addFormDataPart("acceptUid",UserInfoManager.instance.getUserId())
        builder.addFormDataPart("fieldId",selectFieldId)
        builder.addFormDataPart("cropId",cropId.toString())
        builder.addFormDataPart("feedbackContentText",feedbackMes)
        provinceId?.let {
            builder.addFormDataPart("provinceId",provinceId)
        }
        cityId?.let {
            builder.addFormDataPart("cityId",cityId)
        }
        districtId?.let {
            builder.addFormDataPart("districtId",districtId)
        }
        builder.addFormDataPart("feedbackType",feedbackType.toString())
        val parts = builder.build().parts()
        viewModelRes.requestFeedBack(parts,feedbackMessage)
    }
}